<template>
	<view class="signin">
		<view class="topEaBg"></view>
		<view class="text-center padding-top-xl">
			<button v-if="!isSigned" class="cu-btn round bg-white text-red text-bold" @click="signClick">立即签到</button>
			<button v-else class="cu-btn round bg-white text-red text-bold">已签到</button>
			<view class="text-yellow margin-top-sm">点击签到看广告参与签到</view>
			<view class="margin-top text-white">已签到{{qiandaoList.length}}天，继续加油！</view>
		</view>

		<view class="margin" style="border-radius: 10rpx; overflow: hidden;">
			<wn-calendar ref="calendar" :data="qiandaoList" :isBorder="false" :isLess="false"
				:colors="['#e43d33', '#2979ff', '#8f939c', '#18bc37', '#f3a73f']" :isEn="false" format=""
				@change="onChange" @choose="onChoose"></wn-calendar>
		</view>

		<view class="margin padding-sm radius bg-white">
			<text class="text-red text-lg text-bold">每日签到说明</text>
			<view class="margin-top text-red">每日签到是解绑积分的前置条件 。</view>
		</view>

	</view>
</template>

<script>
	import indexApi from '../../../api/new/indexApi'
	// 在页面中定义激励视频广告
	let videoAd = null
	export default {
		data() {
			return {
				qiandaoList: [],
				isSigned: false,
				viewInfo: {
					delay: 180, // 间隔时间
					lastSeeTime: "", // 上次看广时间
					limitNum: "", // 限制次数
					seeTotalNum: "" // 今日已观看次数
				},
			}
		},
		onLoad() {
			this.getQiandaoList()
			// 在页面onLoad回调事件中创建激励视频广告实例
			if (wx.createRewardedVideoAd) {
				videoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-5c44e117606acb59'
				})
				videoAd.onLoad(() => {})
				videoAd.onError((err) => {
					console.error('激励视频光告加载失败', err)
				})
				videoAd.onClose((res) => {
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						console.log("正常结束", res)
						this.addQiandao()
					} else {
						// 播放中途退出，不下发游戏奖励
						console.log("异常退出")
					}
				})
			}
		},
		onPullDownRefresh() {
			this.getQiandaoList()
		},
		methods: {
			signClick() {
				// 用户触发广告后，显示激励视频广告
				indexApi.selectTodayToal().then(res => {
					this.viewInfo = res
					const waitTime = new Date(this.viewInfo.lastSeeTime).getTime() + 180000 - new Date().getTime()
					if (waitTime > 0) {
						uni.showToast({
							title: ((waitTime / 1000).toFixed(0)) + "秒后再次观看",
							icon: "none"
						})
					} else if (this.viewInfo.seeTotalNum == this.viewInfo.limitNum) {
						uni.showToast({
							title: "今日观看次数已用完，明天再来",
							icon: "none"
						})
					} else {
						if (videoAd) {
							videoAd.show().catch(() => {
								// 失败重试
								videoAd.load()
									.then(() => videoAd.show())
									.catch(err => {
										console.error('激励视频 广告显示失败', err)
									})
							})
						}
					}
				})
			},
			addQiandao() {
				indexApi.addQiandao().then(res => {
					console.log("res", res)
					uni.showToast({
						title: "签到成功"
					})
					setTimeout(() => {
						this.getQiandaoList()
					}, 500)
				}).catch(e => {
					uni.showToast({
						title: e,
						icon: "none"
					})
				})
			},
			getQiandaoList() {
				indexApi.myQiandaoList().then(res => {
					this.qiandaoList = []
					res.rows.map(item => {
						this.qiandaoList.push({
							date: this.format(item.createTime),
							text: "已签到",
							type: 0
						})
					})
					const now = this.format(new Date())
					this.isSigned = this.qiandaoList.some(item => now.includes(item.date))
					this.$refs.calendar.refresh()
				})
			},
			format(value) {
				const now = new Date(value)
				const year = now.getFullYear()
				const month = Number(now.getMonth()) + 1
				const date = now.getDate()
				return `${year}/${month}/${date}`
			},
		}
	}
</script>

<style lang="scss">
	.signin {}
</style>